# 在 src/database/dao/base_dao.py 中
from datetime import datetime


class BaseDAO:
    """DAO基类，提供通用功能"""
    
    @staticmethod
    def to_dict(model_obj):
        """将SQLAlchemy模型对象转换为字典"""
        result = {}
        for column in model_obj.__table__.columns:
            value = getattr(model_obj, column.name)
            
            # 处理日期时间
            if isinstance(value, datetime):
                value = value.isoformat()
            
            # 处理JSON字段
            if column.type.python_type == dict and isinstance(value, str):
                try:
                    import json
                    value = json.loads(value)
                except:
                    pass
                    
            result[column.name] = value
        return result
    
    def to_dict_list(self, model_list):
        """将SQLAlchemy模型对象列表转换为字典列表"""
        return [self.to_dict(model) for model in model_list]